$(function () {
    $("#page").load("/html/page.html");
});

function findAll() {
    let data = {
        word: $("#word").val()
        , translate: $("#translate").val()
        , submitter: $("#submitter").val()
        , pageNum: $.trim($("#pageNum").val())
        , pageSize: $.trim($("#pageSize").val())
    }
    let result = myAjax("/w/word/findAll", "post", data);
    if (result.code === 200) {
        showWord(result.data.list);
        $("#pageCount").html(result.data.number);
    } else {
        alert("您没有权限查询");
    }
}

function findLike() {
    let result = myAjax('/w/word/findLike', 'post', {username: username});
    console.log(result);
    return result.data;
}

function findCollect() {
    let result = myAjax('/w/word/findCollect', 'post', {username: username});
    console.log(result);
    return result.data;
}

/**
 * 根据用户名查询出所点赞的 收藏的单词的id，然后在展示的时候去判断，
 * 如果id相等，则点赞和收藏按钮应该带有背景色
 * @param data
 */
function showWord(data) {
    let data1 = findLike();
    let data2 = findCollect();
    console.log(data1);
    console.log(data2);
    let html = "";
    for (let i = 0; i < data.length; i++) {

        html += '   <tr> ' +
            '  <td><input type="checkbox" name="isCheck" value="' + data[i].id + '">' + data[i].id + '</td>\n' +
            '        <td>' + data[i].word + '</td>\n' +
            '        <td>' + data[i].translate + '</td>\n' +
            '        <td>' + data[i].submitter + '</td>\n' +
            '        <td>' + data[i].subTime + '</td>\n' +
            '        <td>' + data[i].heat + '</td>\n' +
            '        <td>  <button name="collect" type="button" onclick="collect(' + data[i].id + ',this)" ' +
            'class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon">收藏</i></button>' +
            '           <button  onclick="like(' + data[i].id + ')" ' +
            'class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-praise">赞</i></button></td>' +
            '    </tr>';
    }
    $("#findAll").html(html);
}

let username = localStorage.getItem("username");

function collect(id,a) {
    let result = myAjax('/w/word/collect', 'post', {id: id, username: username});
    if (result.data === 2) {
        alert("收藏成功！");
        $(a).removeClass("layui-btn layui-btn-primary layui-btn-sm");
        $(a).addClass("layui-btn layui-btn-sm");
    } else if (result.data === 1) {
        alert("取消收藏成功!")
        $(a).removeClass("layui-btn layui-btn-sm");
        $(a).addClass("layui-btn layui-btn-primary layui-btn-sm");
    } else {
        alert("收藏失败，请稍后再试！")
    }
}

function like(id) {
    let result = myAjax('/w/word/like', 'post', {id: id, username: username});
    if (result.data === 200) {
        alert("点赞成功！")
        this.className ='layui-btn layui-btn-sm';
    } else if (result.data === 100) {
        alert("取消点赞成功!")
        this.className ='layui-btn layui-btn-primary layui-btn-sm';
    } else {
        alert("点赞失败，请稍后再试！")
    }
}